//
//  ConRecomendViewController.m
//  Shengzhuli
//
//  Created by FXGuo on 2017/7/13.
//  Copyright © 2017年 kamangkeji. All rights reserved.
//

#import "ConRecomendViewController.h"
#import "YueRoolCell.h"
#import "YueKaCell.h"
#import "YueController.h"
#import "YueKaNetWork.h"
#import "MBProgressHUD+MJ.h"
#import "YueBannerModel.h"
#import "YueController.h"
#import "YueListModel.h"
#import <MJRefresh.h>

@interface ConRecomendViewController ()<UITableViewDelegate,UITableViewDataSource>
@property (nonatomic, strong)UITableView *tableView;
@property (nonatomic, strong)NSMutableArray *dataArray;
@property (nonatomic, strong)NSMutableArray *banners;
@property (nonatomic, assign)NSInteger pageNo;
@end

@implementation ConRecomendViewController

- (void)viewDidLoad {
//    [super viewDidLoad];
//    UIImageView *imgView = [[UIImageView alloc]initWithFrame:CGRectMake(0, 0, ScreenW, ScreenH-64)];
//    imgView.image = GetImage(@"敬请期待");
//    [self.view addSubview:imgView];
    self.pageNo = 1;
    self.navigationItem.title = @"遇见大咖";
    _dataArray = [[NSMutableArray alloc] init];
    [self setUpTableView];
    [self loadData];
    [self loadListData];
}
- (void)loadData {
    [YueKaNetWork getBannerListWithParame:nil response:^(BOOL success, NSString *msg, NSArray *arr) {
        if (success) {
            [self.banners removeAllObjects];
            for (NSMutableDictionary *dict in arr) {
             YueBannerModel *model =[YueBannerModel bannerWithDict:dict];
                [self.banners addObject:model];
            }
                [self.tableView reloadData];
            
        }else {
            [MBProgressHUD showError:msg toView:self.view];
        }
        
    }];
    
}
- (void)loadListData {
    NSMutableDictionary *params = [[NSMutableDictionary alloc] init];
    params[@"pageNo"] = @(self.pageNo);
    [YueKaNetWork getKaListWithParame:params response:^(BOOL success, NSString *msg, NSArray *arr) {
        if (success) {
            [self.tableView.mj_header endRefreshing];
            for (NSMutableDictionary *dict in arr) {
                YueListModel *model= [[YueListModel alloc] init];
                [model updateModelWithDict:dict];
                [_dataArray addObject:model];
            }
            [self.tableView reloadData];
        }else {
            [MBProgressHUD showError:msg toView:self.view];
        }
    }];
}
- (void)setUpTableView
{
    self.tableView = [[UITableView alloc] init];
    self.tableView.separatorInset = UIEdgeInsetsMake(0, 0, 0, 0);
    self.tableView.frame = CGRectMake(0, 0, ScreenW, ScreenH-64);
    self.tableView.delegate = self;
    self.tableView.dataSource = self;
    self.tableView.tableFooterView = [[UIView alloc]init];
    self.tableView.mj_header = [MJRefreshNormalHeader headerWithRefreshingBlock:^{
        _pageNo = 1;
        [_dataArray removeAllObjects];
        [self loadListData];
    }];
    self.tableView.mj_footer = [MJRefreshAutoFooter footerWithRefreshingBlock:^{
        _pageNo++;
        [self loadListData];
    }];
    [self.tableView registerClass:[YueRoolCell class] forCellReuseIdentifier:NSStringFromClass([YueRoolCell class])];
    [self.tableView registerClass:[YueKaCell class] forCellReuseIdentifier:NSStringFromClass([YueKaCell class])];
    [self.view addSubview:self.tableView];
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    
    if (indexPath.row == 0) {
        YueRoolCell *cell = [tableView dequeueReusableCellWithIdentifier:NSStringFromClass([YueRoolCell class])];
        cell.selectionStyle = UITableViewCellSelectionStyleNone;
        cell.banners = self.banners;
        cell.roolBlock = ^(NSInteger index) {
            YueController *yue = [[YueController alloc]init];
            YueBannerModel *model = self.banners[index];
            yue.ID = model.ID;
            [self.navigationController pushViewController:yue animated:YES];
        };
        return cell;
    }else {
        YueKaCell *yueCell = [tableView dequeueReusableCellWithIdentifier:NSStringFromClass([YueKaCell class])];
        YueListModel *model = _dataArray[indexPath.row-1];
        yueCell.selectionStyle = UITableViewCellSelectionStyleNone;
        [yueCell updateCellWithModel:model];
        return yueCell;
    }
    
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    
    return _dataArray.count+1;
}
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
    return 1;
}
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
    if (indexPath.row == 0) {
        return 200;
    }else {
        return 100;
    }
   
}
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
    YueListModel *model = _dataArray[indexPath.row-1];
    YueController *yue = [[YueController alloc] init];
    yue.ID = model.id;
    [self.navigationController pushViewController:yue animated:YES];
}
- (NSMutableArray *)banners
{
    if (!_banners) {
        _banners = [NSMutableArray array];
    }
    return _banners;
}
@end
